<?php

namespace App\Process;

use App\Utility\Cache;
use Carbon\Carbon;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\EasySwoole\Logger;

class DatabaseCopy extends AbstractProcess
{
    private $isRun = false;
    public function run($arg){
        //定时200ms检测有没有任务，有的话就while死循环执行
        $this->addTick(200,function (){
            if(!$this->isRun){
                $this->isRun = true;
                go(function (){
                    while (true){
                        try{
                            $cache = new Cache();
                            $openid_string = $cache->sPop('openid_pushss');
                            if ($openid_string) {
                                $data = explode('*', $openid_string);
                                if (is_array($data)) {
                                    $applet = $data[0];
                                    $openid = $data[1];
                                    //根据openid取找到相应的openid的formid的集合，先删除该集合已过期的所有的formid,
                                    $cache->zRemRangeByScore($openid, 0, time() + 10);
                                    //如果删除集合中过期的formid值后该集合为空，则删除对应的小程序的openid集合中的openid元素
                                    if ($cache->zCard($openid) == 0) $cache->srem($applet, $openid_string);
                                }
                            }
                            unset($cache);
                        }catch (\Throwable $throwable){
                            break;
                        }
                    }
                    $this->isRun = false;
                });
            }
        });
    }

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }

    public function onReceive(string $str, ...$args)
    {
        // TODO: Implement onReceive() method.
    }
}
